package cn.tt.tuantuanerp.tuantuanerp.purchase.webapi.service.impl;


import cn.tt.tuantuanerp.tuantuanerp.comment.ex.ServiceException;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.PurchaseProductAddNewDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.PurchaseProduct;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.PurchaseProductListItemVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.PurchaseProductStandardVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.security.LoginPrincipal;
import cn.tt.tuantuanerp.tuantuanerp.comment.utils.IdGenerator;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.ServiceCode;
import cn.tt.tuantuanerp.tuantuanerp.purchase.service.IPurchaseProductService;
import cn.tt.tuantuanerp.tuantuanerp.purchase.webapi.mapper.PurchaseMapper;
import cn.tt.tuantuanerp.tuantuanerp.purchase.webapi.mapper.PurchaseProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理商品数据的业务实现类
 *
 * @author java@杨波
 * @version 0.0.1
 */
@Slf4j
@Service
public class PurchaseProductServiceImpl implements IPurchaseProductService {
    @Autowired
    PurchaseProductMapper purchaseProductMapper;

    @Autowired
    PurchaseMapper mapper;


    @Override
    public void addNew(List<PurchaseProductAddNewDTO> purchaseProductAddNewDTO) {
        log.debug("开始处理[新增商品]的业务,参数:{}",purchaseProductAddNewDTO);
        List<PurchaseProduct> purchaseProduct = new ArrayList<>();
        List<Long> skuId = new ArrayList<>();
        for (PurchaseProductAddNewDTO item : purchaseProductAddNewDTO){
            PurchaseProduct purchaseProduct1 = new PurchaseProduct();
            BeanUtils.copyProperties(item,purchaseProduct1);
            purchaseProduct.add(purchaseProduct1);
            skuId.add(item.getSkuId());
        }
        int rows = purchaseProductMapper.insertBatch(purchaseProduct);
        log.debug("插入商品数据成功,插入{}条",rows);
    }

    @Override
    public void addNew(PurchaseProductAddNewDTO purchaseProductAddNewDTO) {
        PurchaseProduct purchaseProduct = new PurchaseProduct();
        BeanUtils.copyProperties(purchaseProductAddNewDTO,purchaseProduct);
        purchaseProductMapper.insert(purchaseProduct);
    }

    @Override
    public List<PurchaseProductListItemVO> list() {
        log.debug("开始处理[查询商品列表]的业务");
        List<PurchaseProductListItemVO> list = purchaseProductMapper.list();
        return list;
    }

    @Override
    public PurchaseProductStandardVO getStandardBySkuId(Long skuId) {
        log.debug("开始处理[根据商品编号查询商品详情]的业务.参数:{}",skuId);
        PurchaseProductStandardVO standardBySkuId = purchaseProductMapper.getStandardBySkuId(skuId);
        return standardBySkuId;
    }

    @Override
    public PurchaseProductStandardVO getByPurchaseId(String purchaseId) {
        log.debug("开始处理[根据id]查询商品信息的业务");
        PurchaseProductStandardVO standardByPurchaseId = purchaseProductMapper.getStandardByPurchaseId(purchaseId);
        return standardByPurchaseId;
    }

    @Override
    public String returnPurchaseId(LoginPrincipal loginPrincipal) {
        String longDate = IdGenerator.getPurchaseId(loginPrincipal);
        log.debug("得到随机生成的商品id:{}",longDate);
        PurchaseProductStandardVO standardVO = purchaseProductMapper.getByPurchaseId(longDate);
        if (standardVO!=null){
            String message = "服务器繁忙!";
            log.warn(message);
            throw new ServiceException(ServiceCode.Err_IS_EXIST,message);
        }
        return longDate;
    }

}
